package omg.loot;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.media.opengl.GL2;

public class SpriteBatch {
   /** The buffer holding the indices */
   public static ShortBuffer indexBuffer;
   public static FloatBuffer vertices;
   public static FloatBuffer texCoords;
   protected int size;
   protected float[] vt;
   float x1, y1, x2, y2;
   int indx, actualCount;

   public SpriteBatch() {};

   public SpriteBatch(final int maxBatchSize) {
      final int len = maxBatchSize * 6;
      final short[] indices = new short[len];
      vt = new float[maxBatchSize*8];
      short j = 0;
      for (int i = 0; i < len; i += 6, j += 4) {
         indices[i]     = j;
         indices[i + 1] = (short)(j + 1);
         indices[i + 2] = (short)(j + 2);
         indices[i + 3] = j;
         indices[i + 4] = (short)(j + 2);
         indices[i + 5] = (short)(j + 3);
      }
      //Luodaan indices-bufferi:
      final ByteBuffer buf = ByteBuffer.allocateDirect(indices.length * 2);
      buf.order(ByteOrder.nativeOrder());
      indexBuffer = buf.asShortBuffer();
      indexBuffer.put(indices);
      indexBuffer.position(0);
      //Luodaan vertexBufferi:
      final ByteBuffer byteBuf2 = ByteBuffer.allocateDirect(vt.length * 4);
      byteBuf2.order(ByteOrder.nativeOrder());
      vertices = byteBuf2.asFloatBuffer();
      //Luodaan textuuriBufferi:
      final ByteBuffer byteBuf1 = ByteBuffer.allocateDirect(vt.length * 4);
      byteBuf1.order(ByteOrder.nativeOrder());
      texCoords = byteBuf1.asFloatBuffer();
   }

   public void drawBatch(final GL2 gl, final Sprite texture, final Entity[] e, final int count) {
      indx = 0;
      actualCount = 0;
      for (int i = 0; i < count; i++) {
         x1 = (float) (e[i].x*Tile.size-e[i].r);
         y1 = (float) (e[i].y*Tile.size-e[i].r);
         x2 = (float) (x1+e[i].r*2f);
         y2 = (float) (y1+e[i].r*2f);
         vt[indx++] = x1;  vt[indx++] = y1;  // 0, Top Left
         vt[indx++] = x1;  vt[indx++] = y2;  // 1, Bottom Left
         vt[indx++] = x2;  vt[indx++] = y2;  // 2, Bottom Right
         vt[indx++] = x2;  vt[indx++] = y1;  // 3, Top Right
         actualCount++;
      }
      vertices.rewind();
      vertices.put(vt, 0, indx);
      vertices.position(0);
      gl.glVertexPointer(2, GL2.GL_FLOAT, 0, vertices);
      gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, texture.texCoords);
      gl.glDrawElements(GL2.GL_TRIANGLES, actualCount*6, GL2.GL_UNSIGNED_SHORT, indexBuffer);
   }

   public void drawBatch(final GL2 gl, final Renderer render, final Tile[][] e) {
      indx = 0;
      actualCount = 0;
      for (int i = 0; i < e.length; i++) {
         for (int j = 0; j < e[i].length; j++) {
            switch (e[i][j].type) {
               case Tile.LAND:
                  texCoords.put(render.tile_land.texCoords);
                  render.tile_land.texCoords.position(0);
                  break;
               case Tile.WATER:
                  texCoords.put(render.tile_water.texCoords);
                  render.tile_water.texCoords.position(0);
                  break;
            }
            x1 = (e[i][j].x*Tile.size-Tile.r);
            y1 = (e[i][j].y*Tile.size-Tile.r);
            x2 = (x1+Tile.size);
            y2 = (y1+Tile.size);
            vt[indx++] = x1;  vt[indx++] = y1;  // 0, Top Left
            vt[indx++] = x1;  vt[indx++] = y2;  // 1, Bottom Left
            vt[indx++] = x2;  vt[indx++] = y2;  // 2, Bottom Right
            vt[indx++] = x2;  vt[indx++] = y1;  // 3, Top Right
            actualCount++;
         }
      }
      vertices.rewind();
      vertices.put(vt, 0, indx);
      vertices.position(0);
      texCoords.position(0);
      gl.glVertexPointer(2, GL2.GL_FLOAT, 0, vertices);
      gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, texCoords);
      gl.glDrawElements(GL2.GL_TRIANGLES, actualCount*6, GL2.GL_UNSIGNED_SHORT, indexBuffer);
   }
}